/**
 * @Description:功能描述
 * @Author:zqy
 * @Date:2023-05-24 17:51:49
 * @Last Modified by: zqyy
 * @Last Modified time: 2023-05-24 17:52:232
 */

/**
 * @class 监听虚拟键盘
 * @classdesc 监听虚拟键盘弹出隐藏
 * @public onEnd 结束监听虚拟键盘
 * @public onShow 传递一个回调 监听虚拟键盘弹出
 * @public onHidden 传递一个回调 监听虚拟键盘隐藏
 */
class MonitorKeyboard {
  type: number
  originalHeight: number
  show: any
  hidden: any
  constructor() {
    this.type = this.checkDeviceType()
    this.originalHeight = document.documentElement.clientHeight || document.body.clientHeight // window.innerHeight
  }

  /**
   *  @function checkDeviceType 获取设备类型
   *  @param  1 Android 2 iOS
   */
  checkDeviceType = () => {
    const userAgent = typeof window === 'object' ? window.navigator.userAgent : ''
    if (/android/i.test(userAgent)) {
      return 1
    } else if (/iPhone|iPod|iPad/i.test(userAgent)) {
      return 2
    }
    return 0
  }

  // Android系统，获取窗口的高度
  onResize = () => {
    //键盘弹起与隐藏都会引起窗口的高度发生变化
    const resizeHeight = document.documentElement.clientHeight || document.body.clientHeight // window.innerHeight

    if (this.originalHeight - resizeHeight > 50) {
      this.show('Android系统: 软键盘弹出')
    } else {
      this.hidden('Android系统: 软键盘收起')
    }
  }

  // iOS获取焦点
  onFocusin = () => {
    this.show('iOS系统:软键盘弹出')
  }

  // iOS失去焦点
  onFocusout = () => {
    this.hidden('iOS系统:软键盘收起')
  }

  /**
   * @function onStart 开始监听虚拟键盘
   */
  onStart = () => {
    if (this.type == 1) {
      // Android系统
      window.addEventListener('resize', this.onResize, true)
    }
    if (this.type == 2) {
      // iOS系统
      window.addEventListener('focusin', this.onFocusin)
      window.addEventListener('focusout', this.onFocusout)
    }
    // console.log('开启监听', this.type)
  }

  /**
   * @function onEnd 结束监听虚拟键盘
   */
  onEnd = () => {
    // console.log('移除监听removeEventListener', this.type)
    if (this.type == 1) {
      // Android系统
      window.removeEventListener('resize', this.onResize, true)
    }
    if (this.type == 2) {
      window.removeEventListener('focusin', this.onFocusin)
      window.removeEventListener('focusout', this.onFocusout)
    }
  }

  /**
   * @function  onShow 传递一个回调函数
   * @param  虚拟键盘弹出时触发
   */
  onShow = (fn: any) => {
    this.show = fn
  }

  /**
   * @function  onHidden 传递一个回调函数
   * @param  虚拟键盘隐藏时触发
   */
  onHidden = (fn: any) => {
    this.hidden = fn
  }
}

export default MonitorKeyboard
